package edu.dayu.demo3;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

class AuthenticationManager {
    public static void main(String[] args) {
        AuthenticationManager manager = new AuthenticationManager(28);
        manager.generate("aaa",7);
        manager.generate("bbb",20);
        System.out.println(manager.countUnexpiredTokens(23));
    }


    int timeToLive;
    Map<String,Integer> map = new HashMap();

    public AuthenticationManager(int timeToLive) {
        this.timeToLive = timeToLive;
    }

    public void generate(String tokenId, int currentTime) {
        map.put(tokenId,currentTime);
    }

    public void renew(String tokenId, int currentTime) {
        Integer str = map.get(tokenId);
        if(str!= null&&str!=0){
            if(currentTime<str+timeToLive)
                map.put(tokenId,currentTime);
        }
    }

    public int countUnexpiredTokens(int currentTime) {
        int i =0;
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Integer> entry = iterator.next();
            if(currentTime<entry.getValue()+timeToLive)
                i++;
        }
        return i;
    }
}

